import request from '@/utils/request'
import { ApiModulesRoutes } from '@/consts'

/**
 * 查询设备列表（分页）
 * @param {'CAMERA','ROBOT'} data.deviceTypeEnum
 * @param {'DISABLE','ENABLE'}  data.enableStateEnum
 * @param {'ALG','COURSE','DEFAULT','DEVICE','GATEWAY','REGION','SECURITY','TRAIL','USER'}  data.excludeBusinessTypeEnum
 * @param {String}  data.gatewayId
 * @param {String}  data.keyword
 * @param {'DIRECT','FIVE_G','GATEWAY'}  data.netTypeEnum
 * @param {Boolean}  data.alg
 * @param {Boolean}  data.location
 * @param {Boolean}  data.online
 * @param {Number}  data.pageNo
 * @param {Number}  data.pageSize
 * @param {String}  data.productId
 * @param {String}  data.regionId
 */

export function apiFindDevicePageByParam(data) {
  return request({
    url: `${ApiModulesRoutes.IOT}/device/findDevicePageByParamNew`,
    method: 'POST',
    data
  })
}

/**
 * 根据设备id删除设备
 * @param {String} deviceId 设备id
 * @returns {Promise<AxiosResponse<any>>}
 */
export const apiDelDeviceById = async deviceId => {
  const url = ApiModulesRoutes.IOT + '/device/delDeviceById'
  return await request.get(url, { params: deviceId })
}

/**
 * 添加设备
 * @param deviceVo
 * @param {String} deviceVo.name 设备名称
 * @param {String} deviceVo.parentId 父设备
 * @param {String} deviceVo.productId 所属产品
 * @param {'OFFLINE','ONLINE'} deviceVo.onlineState 启用状态
 * @param {String} deviceVo.remake 备注说明
 * @returns {Promise<AxiosResponse<any>>}
 */
export const apiAddDevice = async deviceVo => {
  const url = ApiModulesRoutes.IOT + '/device/addDevice'
  return await request.post(url, deviceVo)
}

/**
 * 根据设备id获取设备详情
 * @param {String} deviceId
 * @returns {Promise<void>}
 */
export const apiFindDeviceById = async deviceId => {
  const url = ApiModulesRoutes.IOT + '/device/findDeviceById'
  return await request.get(url, { params: { deviceId } })
}

export const apiUpdateDevice = async device => {
  const url = ApiModulesRoutes.IOT + '/device/updateDevice'
  return await request.post(url, device)
}

/**
 * 打开视频流
 * @param {String} data.deviceId
 * @param {String} data.ip
 */
export const apiOpenPlayer = async data => {
  const url = ApiModulesRoutes.IOT + '/camera/openCamera'
  return request.get(url, {
    params: data
  })
}

/**
 * 查询物模型属性列表
 * @param {String} data.metadataId
 * @param {String} data.pageNo
 * @param {String} data.pageSize
 */
export function findPropertyList(data) {
  return request({
    url: `${ApiModulesRoutes.IOT}/property/findPropertyPageByParam`,
    method: 'POST',
    data
  })
}

/**
 * 查询物模型服务列表
 * @param {String} data.metadataId
 * @param {String} data.pageNo
 * @param {String} data.pageSize
 */
export function findServiceList(data) {
  return request({
    url: `${ApiModulesRoutes.IOT}/Services/findServicesPageByParam`,
    method: 'POST',
    data
  })
}

/**
 * 获取字段值
 * @param {String} fieldId
 */

export function findPropertyValueById(fieldId) {
  return request({
    url: `${ApiModulesRoutes.IOT}/propertyValue/findPropertyValueById`,
    method: 'GET',
    params: { fieldId }
  })
}

/**
 * 新增字段值
 * @param {String} data.content
 * @param {String} data.deviceId
 * @param {String} data.proId
 * @param {String} data.id
 */

export function addOrUpdatePropertyValue(data) {
  return request({
    url: `${ApiModulesRoutes.IOT}/propertyValue/addOrUpdatePropertyValue`,
    method: 'POST',
    data
  })
}

/**
 * 设定设备位置信息
 * @param locationVo
 * @param {String} locationVo.businessKey 设备Id
 * @param{String} locationVo.longitude 位置经度
 * @param {String} locationVo.latitude 位置纬度
 * @return {Promise<AxiosResponse<any>>}
 */
export const apiSetDeviceLocation = async (locationVo) => {
  const url = ApiModulesRoutes.IOT + '/device/setDeviceLocation'
  return request.post(url, locationVo)
}

/**
 * 获取所有产品
 */

export function findPList() {
  return request({
    url: `${ApiModulesRoutes.IOT}/product/findPList`,
    method: 'GET',
  })
}

/**
 * 下载设备导入模板
 * @param {String} productId
 */

export function deviceTemplateExcel(productId) {
  return request({
    url: `${ApiModulesRoutes.IOT}/device/deviceTemplateExcel`,
    responseType: 'blob',
    method: 'GET',
    params:{productId}
  })
}

/**
 * 检测列表是否符合规定
 */

export function checkoutDeviceExcel(file) {
  return request({
    url: `${ApiModulesRoutes.IOT}/device/checkoutDeviceExcel`,
    headers: { 'Content-Type': 'multipart/form-data' },
    method: 'POST',
    data:file
  })
}

/**
 * 上传列表
 */

export function importDevicesExcel(file) {
  return request({
    url: `${ApiModulesRoutes.IOT}/device/importDevicesExcel`,
    headers: { 'Content-Type': 'multipart/form-data' },
    method: 'POST',
    data:file
  })
}
